import { reactive } from 'vue'
import {getPostList, getBloggerInfo, getTagList, getCategoryList} from '@/api/customer'
import { getToken, setToken, removeToken } from '@/utils/auth'
import router from '@/router'
import {login} from "@/api/admin";


export const homePageData = reactive({
    homePostList: [],
    pageNum: 1,
    pageSize: 10,
    total: 0,
    getList(page: any, limit: any) {
        this.pageNum = page;
        this.pageSize = limit;
        getPostList({'pageNum': this.pageNum, 'pageSize': this.pageSize}).then(
            (res) => {
                console.log(res)
                this.total = res.data.count;
                this.homePostList = res.data.results
            }
        )
    }
})

export const tagsPageData = reactive({
    tagsPostList: [],
    pageNum: 1,
    pageSize: 10,
    total: 0,
    tagId:null,
    getList(page: any, limit: any,tagId:any) {
        this.pageNum = page;
        this.pageSize = limit;
        this.tagId = tagId;
        getPostList({'pageNum': this.pageNum, 'pageSize': this.pageSize,'tagId':this.tagId}).then(
            (res) => {
                console.log(res)
                this.total = res.data.count;
                this.tagsPostList = res.data.results
            }
        )
    }
})

export const categoryPageData = reactive({
    categoryPostList: [],
    pageNum: 1,
    pageSize: 10,
    total: 0,
    categoryId: null,
    getList(page: any, limit: any,categoryId:any) {
        this.pageNum = page;
        this.pageSize = limit;
        this.categoryId = categoryId;
        getPostList({'pageNum': this.pageNum, 'pageSize': this.pageSize,'categoryId':this.categoryId}).then(
            (res) => {
                 this.total = res.data.count;
                this.total = res.data.total;
                this.categoryPostList = res.data.results
            }
        )
    }
})

export const bloggerInfo = reactive({
    isLoad : false,
    bloggerName : 'Loading...',
    description : 'Loading...',
    avatar:'',
    societies : [],
    postNum : 0,
    postCategoryNum : 0,
    postTagNum : 0,
    getInfo(){
        if(!this.isLoad){
            getBloggerInfo().then((res)=>{
                var data = res.data;
                var bloggers = data.bloggers;
                bloggers.forEach((item:any)=>{
                    if(item.key==='name') this.bloggerName = item.value;
                    if(item.key==='description') this.description = item.value;
                    if(item.key==='avatar') this.avatar = item.value;
                })
                var societies = data.societies;
                societies.forEach((item:any)=>{
                    this.societies[item.key] = item.value;
                })
                //this.societies = data.societies;
                this.postNum = data.postNum;
                this.postCategoryNum = data.postCategoryNum;
                this.postTagNum =  data.postTagNum;
            })
            if(tagList.list.length<=0){
                tagList.getTagList()
            }
            if(categoryList.list.length<=0){
                categoryList.getCategoryList()
            }
        }
    }
})

export const tagList =reactive({
    list :[],
    getTagList(){
        getTagList().then((res)=>{
            console.log(res)
            this.list = res.data
        })
    }
})
export const categoryList = reactive({
    list:[],
    getCategoryList(){
        getCategoryList().then((res)=>{
            console.log(res)
            this.list = res.data
        })
    }
})

export const adminInfo = reactive({
    token:getToken(),
    login(userInfo:any){
        const { username, password } = userInfo
        login({ username: username.trim(), password: password }).then(response=>{
            const { data } = response
            this.token = data.token
            setToken(data.token)
            router.push({ path: '/admin'});
        })
    },
    logout(userInfo:any){

    }
})